package gpl.androidcommons.service;

import gpl.scotlandyard.beans.basics.Node;
import gpl.scotlandyard.services.utils.MapLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

import android.content.res.AssetManager;

public class MapLoaderPerformer extends ScheduleService<List<Node>> {

  private final AssetManager assetManager;
  private final String file;

  public MapLoaderPerformer(AssetManager assetManager, String file, ScheduleServiceListener<List<Node>> listener) {
    super(listener);
    this.assetManager = assetManager;
    this.file = file;
  }

  @Override
  public List<Node> perform() throws IOException {
    BufferedReader bufr = new BufferedReader(new InputStreamReader(assetManager.open(file, AssetManager.ACCESS_BUFFER)));
    List<Node> map = new LinkedList<Node>();
    map.addAll(MapLoader.loadMap(bufr));
    return map;
  }

}
